package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.PayInfoDTO;
import com.pshcp.domain.model.PayInfo;
import com.pshcp.mapper.PayInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 支付记录 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-12-20
 */
@Service
public class PayInfoService {

    @Autowired
    private PayInfoMapper payInfoMapper;

    public PageList<PayInfo> list(PayInfoDTO payInfoDTO){
        if(payInfoDTO.getPageNum() != null && payInfoDTO.getPageSize() != null){
            PageHelper.startPage(payInfoDTO.getPageNum(), payInfoDTO.getPageSize());
        }
        Example example = new Example(PayInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if(payInfoDTO.getStatus() != null){
            criteria.andEqualTo("status", payInfoDTO.getStatus());
        }else{
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        example.setOrderByClause(" created_time desc");
        List<PayInfo> list = payInfoMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<PayInfo> select(PayInfo payInfo){
        return payInfoMapper.select(payInfo);
    }

    public int insert(PayInfo payInfo){
        payInfo.setId(SystemUtil.getId());
        payInfo.setStatus(CommonStatusEnum.INIT.getCode());
        payInfo.setCreatedTime(new Date());
        return payInfoMapper.insert(payInfo);
    }

    public int update(PayInfo payInfo){
        payInfo.setUpdateTime(new Date());
        return payInfoMapper.updateByPrimaryKeySelective(payInfo);
    }

    public PayInfo findById(Long id){
        return payInfoMapper.selectByPrimaryKey(id);
    }

    public PayInfo findByBizIdAndPayBiz(Long bizId, Integer payBiz){
        Example example = new Example(PayInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bizId", bizId);
        criteria.andEqualTo("payBusiness", payBiz);
        example.setOrderByClause(" created_time desc");
        List<PayInfo> payInfos = payInfoMapper.selectByExample(example);
        return CollectionUtils.isEmpty(payInfos) ? null : payInfos.get(0);
    }

    public List<PayInfo> findByIds(List<Long> ids){
        Example example = new Example(PayInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return payInfoMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(PayInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PayInfo payInfo = new PayInfo();
        payInfo.setStatus(CommonStatusEnum.DEL.getCode());
        return payInfoMapper.updateByExampleSelective(payInfo, example);
    }

}
